package cn.com.scitc.licenseplateback.verificationCode;

import cn.com.scitc.licenseplateback.redis.RedisUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class SendSms {

    @Autowired
    RedisUtil redisUtil;

    public String send(RequestData requestData) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FcKKojRXNBFKLwbTJYB", "NFhKdsDATiBA3huWwoEcRZDSFEyAgd");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", requestData.getPhoneNumbers());
        request.putQueryParameter("SignName", "立方库智能停车系统");
        request.putQueryParameter("TemplateCode", "SMS_173951012");
        request.putQueryParameter("TemplateParam", "{code:'" + requestData.getTemplateParam() + "'}");

        String statusText = "";

        try {
            CommonResponse response = client.getCommonResponse(request);

            MessageCheck messageCheck = JSONObject.parseObject(response.getData(), MessageCheck.class);
            System.out.println(response.getData());

            if (messageCheck.getCode().equals("OK")) {
                redisUtil.set(requestData.getPhoneNumbers(), requestData.getTemplateParam());
                redisUtil.expire(requestData.getPhoneNumbers(), 300);
            } else {
                statusText = messageCheck.getMessage();
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        // 在这里返回，主要是用于 测试 。。。 以上注释代码才是真正的开发代码
//        redisUtil.set(requestData.getPhoneNumbers(), requestData.getTemplateParam());
//        redisUtil.expire(requestData.getPhoneNumbers(), 300);
        return statusText;
    }
}